import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      component: () => import("../views/HomeView.vue"),
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/cart",
      name: "cart",
      component: () => import("../views/Cart.vue"),
    },
    {
      path: "/user",
      name: "user",
      component: () => import("../views/User.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/LoginView.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/login/Register.vue"),
    },
    {
      path: "/registerNext",
      name: "registerNext",
      component: () => import("../views/login/RegisterNext.vue"),
    },
    {
      path: "/registerContinue",
      name: "registerContinue",
      component: () => import("../views/login/RegisterContinue.vue"),
    },
    {
      path: "/addressManagement",
      name: "addressManagement",
      component: () => import("../views/user/AddressManagement.vue"),
    },
    {
      path: "/orderCenter",
      name: "orderCenter",
      // component: () => import("../views/user/OrderCenter.vue"),
      component: () => import("../views/user/OrderCenter.vue"),
    },
    {
      path:"/search",
      name:"search",
      component:()=>import("../views/search/Search.vue")
    },
    {
      path:"/searchResult",
      name:"searchResult",
      component:()=>import("../views/search/SearchResult.vue")
    },
    {
      path: "/:pathMatch(.*)",
      meta: {
        flag: true,
      },
      component: () => import("../views/404.vue"),
    },
  ],
});

// 前置路由守卫==》登录防跳功能
router.beforeEach((to, from) => {
  if (to.fullPath === "/login") {
    // 判断是否已经登录
    if (localStorage.getItem("token")) {
      return { path: "/home" };
    } else {
      return true;
    }
  } else {
    // 判断是否已经登录
    if (localStorage.getItem("token")) {
      return true;
    } else {
      // return { path: "/login" };
    }
  }
});

export default router;
